/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package table;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import session.ResTableFacadeLocal;

/**
 *
 * @author TANGQUOCTUAN
 */
public class ControllerAdminUpdateTable extends HttpServlet {

    @EJB
    private ResTableFacadeLocal resTableFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Integer seat = 0;
            Integer num = 0;
            Integer id = 0;
            String des = "";
            String itemName = "";
            String picture = "";
            String hinh = "";

            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (!isMultipart) {
                out.println("not a multipart");
            } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                try {
                    items = upload.parseRequest(request);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if (item.isFormField()) {
                        String name = item.getFieldName();
                        String value = item.getString();
                        if (name.equals("txtseat")) {
                            seat = new Integer(value.trim());
                        }
                        if (name.equals("txtids")) {
                            id = new Integer(value.trim());
                        }
                        if (name.equals("txtdes")) {
                            des = value.trim();
                        }
                        if (name.equals("txtnum")) {
                            num = new Integer(value.trim());
                        }
                        if (name.equals("picture")) {
                            picture = (String) value.trim();
                        }
                    } else {
                        try {
                            itemName = item.getName();
                            if (itemName.equals("")) {
                                hinh = picture;
                            } else {
                                File ff = new File(itemName);
                                itemName = System.currentTimeMillis() + ff.getName();
                                String saveFile = getServletContext().getRealPath("");
                                saveFile = saveFile.substring(0, saveFile.lastIndexOf("\\default") + 8);
                                saveFile += "/deploy/images.war/" + itemName;
                                System.err.println("___________" + saveFile);
                                File savedFile = new File(saveFile);
                                item.write(savedFile);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                if (itemName.equals("")) {
                    this.resTableFacade.updateTable(num, seat, id, des, hinh);
                    response.sendRedirect("ControllerTables?action=AdminTables");
                } else {
                    String path = "http://localhost:8080/images/" + itemName;
                    this.resTableFacade.updateTable(num, seat, id, des, path);
                    response.sendRedirect("ControllerTables?action=AdminTables");
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
